package work;

import jxl.Cell;
import jxl.Sheet;
import jxl.Workbook;
import utils.DateUtils;
import utils.HideUtils;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import static utils.HideUtils.TYPE_ID_NUM;
import static utils.HideUtils.TYPE_NAME;
import static utils.HideUtils.TYPE_PHONE;

public class ImageGenerator {

    public static void main(String[] args) throws Exception {
        String baseDir = "/Users/lining/Desktop/CreateImage/";
        String dataXlsDir = baseDir + "data.xls";
        String imageDir = baseDir + "arbiAssetInfoTemplate.jpg";
        String workDir = baseDir + DateUtils.formatDate("yyyyMMdd_HHmmss") + "/";
        new File(workDir).mkdir();
        List<Map<String, String>> dataList = genImageDataList(dataXlsDir);
        ExecutorService executorService = Executors.newFixedThreadPool(10);
        dataList.forEach(data -> {
            executorService.execute(() -> {
                getScreenShotsFileInfo(data, workDir, imageDir);
            });
        });
        executorService.shutdown();
    }

    public static void getScreenShotsFileInfo(Map<String,String> imageData,String baseDir,String imageDir){
        try {
            String targetImageDir = baseDir + imageData.get("title") + "/";
            new File(targetImageDir).mkdir();
            InputStream inputStream = new FileInputStream(new File(imageDir));
            BufferedImage buffImg = ImageIO.read(inputStream);
            Graphics2D g2 =  buffImg.createGraphics();
            g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);//抗锯齿
            Color c1 = new Color(51,51,51);
            g2.setColor(c1);
            //g2.setFont(new Font("sans-serif",1,16));
            g2.setFont(getSimSunFont(16));
            g2.drawString(imageData.get("title"),18,20); // 资产编号
            //Font f2 = new Font("sans-serif",0,13);
            Font f = getSimSunFont(13);
            g2.setColor(new Color(255,0,0));
            g2.setFont(f);
            g2.drawString(imageData.get("needRepayAmount"),697,60); // 逾期金额
            g2.drawString(imageData.get("owedAmount"),868,60); // 欠款金额
            g2.setColor(c1);
            g2.drawString(HideUtils.desensitize(TYPE_NAME,imageData.get("borrowerName")),58,133); //姓名
            g2.drawString(imageData.get("borrowerSex"),223,133); //性别
            g2.drawString(HideUtils.desensitize(TYPE_ID_NUM,imageData.get("borrowerIdNum")),367,133); //身份证号码
            g2.drawString(HideUtils.desensitize(TYPE_PHONE,imageData.get("borrowerTel")),716,133); //手机号码
            g2.drawString(imageData.get("borrowerResidence"),196,168); //地址
            g2.drawString(imageData.get("borrowerPermanent"),196,197); //户籍地址
            g2.drawString(imageData.get("assetCollectResult"),260,230); //催收等级
            g2.drawString(imageData.get("assetLateDays"),416,230); //逾期天数
            g2.drawString(imageData.get("assetGrantAt"),570,230); //放宽日
            g2.drawString(imageData.get("assetLoanChannel"),810,230); //资金渠道
            g2.drawString(imageData.get("overduePrincipalAmount"),236,295); //应还本金
            g2.drawString(imageData.get("overdueInterestAmount"),370,295); //应还利息
            g2.drawString(imageData.get("overdueFeeAmount"),490,295); //应还手续费
            g2.drawString(imageData.get("overduePenaltyAmount"),713,295); //应还违约金
            g2.drawString(imageData.get("repaidPrincipalAmount"),236,328); //已还本金
            g2.drawString(imageData.get("repaidInterestAmount"),370,328); //已还利息
            g2.drawString(imageData.get("repaidFeeAmount"),490,328); //已还手续费
            g2.drawString(imageData.get("repaidPenaltyAmount"),713,328); //已还违约金
            g2.drawString(imageData.get("needRepayPrincipalAmount"),236,362); //待还本金
            g2.drawString(imageData.get("needRepayInterestAmount"),370,362); //待还利息
            g2.drawString(imageData.get("needRepayFeeAmount"),490,362); //待还手续费
            g2.drawString(imageData.get("needRepayPenaltyAmount"),713,362); //待还违约金

            String fileLocalPath = targetImageDir + imageData.get("borrowerName")+ ".jpg";

            File file = new File(fileLocalPath);
            ImageIO.write(buffImg,"JPEG",file);
            inputStream.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static List<Map<String, String>> genImageDataList(String dir) throws Exception {
        Sheet sheet = Workbook.getWorkbook(new File(dir)).getSheet(0);
        Cell[] titleArr = sheet.getRow(0);
        List<Map<String, String>> dataList = new ArrayList<>();
        for (int r = 1; r < sheet.getRows(); r++) {
            Cell[] dataArr = sheet.getRow(r);
            Map<String, String> data = new HashMap<>();
            dataList.add(data);
            for (int i = 0; i < dataArr.length; i++) {
                data.put(titleArr[i].getContents(), dataArr[i].getContents());
            }
        }
        return dataList;
    }


    public static Font getSimSunFont(float fontSize) throws Exception{
        InputStream inputStream = new FileInputStream(new File("/Users/lining/Desktop/CreateImage/simsun.ttf"));
        Font font = Font.createFont(Font.TRUETYPE_FONT, inputStream);
        Font fontBase = font.deriveFont(fontSize);
        GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
        ge.registerFont(fontBase);
        return fontBase;
    }
}
